/**
 * Copyright (C) 2011 Computer Science Department, Loei Rajabhat University
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package th.ac.lru.cs.ktd.keyboard;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;

import th.ac.lru.cs.ktd.frame.DictionaryMainFrame;

public class KoreanVirtualKeyboard extends JDialog {

	private static final long serialVersionUID = -8405515757972089937L;
	
	private final String EMPTY_STRING = "";
	
	private JPanel mKoreanKeyboardContentPane;
	private JTextField mTextFieldKoreanWord;
	
	private JButton mButtonNiEun;
	private JButton mButtonTiEud;
	private JButton mButtonRiEul;
	private JButton mButtonMiEum;
	private JButton mButtonSpacbar;
	private JButton mButtonPiEub;
	private JButton mButtonSiEud;
	private JButton mButtonEiEung;
	private JButton mButtonChiEud;
	private JButton mButtonHiEud;
	private JButton mButtonAnYe;
	private JButton mButtonAYeo;
	private JButton mButtonOYo;
	private JButton mButtonUYeu;
	private JButton mButtonEiEu;
	private JButton mButtonClearLastWord;
	private JButton mButtonClearAll;
	private JButton mButtonEnter;
	
	private DictionaryMainFrame mParentFrame;

	private static KoreanVirtualKeyboardAutomata mAutomata;
	private static KoreanVirtualKeyboard mVirtualKeyboardInstance;
	
	public static KoreanVirtualKeyboard getInstance(DictionaryMainFrame parent) {
		if (mVirtualKeyboardInstance == null) {
			mVirtualKeyboardInstance = new KoreanVirtualKeyboard();
			mVirtualKeyboardInstance.mParentFrame = parent;
			mAutomata = new KoreanVirtualKeyboardAutomata();
			mVirtualKeyboardInstance.clear();
		}
		return mVirtualKeyboardInstance;
	}

	/**
	 * Create the new frame.
	 */
	private KoreanVirtualKeyboard() {
		setBackground(SystemColor.inactiveCaption);
		setTitle("Korean Thai Dictionary - Korean Virtual Keyboard");
		setResizable(false);
		setAlwaysOnTop(true);

		setBounds(100, 100, 570, 321);
		mKoreanKeyboardContentPane = new JPanel();
		mKoreanKeyboardContentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(mKoreanKeyboardContentPane);
		mKoreanKeyboardContentPane.setLayout(null);
		
		mTextFieldKoreanWord = new JTextField();
		mTextFieldKoreanWord.setBounds(10, 11, 440, 62);
		mKoreanKeyboardContentPane.add(mTextFieldKoreanWord);
		mTextFieldKoreanWord.setColumns(10);
		mTextFieldKoreanWord.setEditable(false);
		mTextFieldKoreanWord.setBackground(Color.white);
		JButton mButtonKiYeonk = new JButton("ㄱㅋㄲ");
		mButtonKiYeonk.setFont(UIManager.getFont("Button.font"));
		mButtonKiYeonk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(0);
			}
		});
		mButtonKiYeonk.setBounds(10, 84, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonKiYeonk);
		
		mButtonNiEun = new JButton("ㄴ");
		mButtonNiEun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(2);
			}
		});
		mButtonNiEun.setBounds(100, 84, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonNiEun);
		
		mButtonTiEud = new JButton("ㄷㅌ");
		mButtonTiEud.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(3);
			}
		});
		mButtonTiEud.setBounds(190, 84, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonTiEud);
		
		mButtonRiEul = new JButton("ㄹ");
		mButtonRiEul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(5);
			}
		});
		mButtonRiEul.setBounds(280, 84, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonRiEul);
		
		mButtonMiEum = new JButton("ㅁ");
		mButtonMiEum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(6);
			}
		});
		mButtonMiEum.setBounds(370, 84, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonMiEum);
		
		mButtonSpacbar = new JButton("띄어쓰기");
		mButtonSpacbar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(-1);
			}
		});
		mButtonSpacbar.setBounds(460, 84, 94, 59);
		mKoreanKeyboardContentPane.add(mButtonSpacbar);
		
		mButtonPiEub = new JButton("ㅂㅍㅃ");
		mButtonPiEub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(7);
			}
		});
		mButtonPiEub.setBounds(10, 154, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonPiEub);
		
		mButtonSiEud = new JButton("ㅅㅆ");
		mButtonSiEud.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(9);
			}
		});
		mButtonSiEud.setBounds(100, 154, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonSiEud);
		
		mButtonEiEung = new JButton("ㅇ");
		mButtonEiEung.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appendText(11);
			}
		});
		mButtonEiEung.setBounds(190, 154, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonEiEung);
		
		mButtonChiEud = new JButton("ㅈㅊㅉ");
		mButtonChiEud.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(12);
			}
		});
		mButtonChiEud.setBounds(280, 154, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonChiEud);
		
		mButtonHiEud = new JButton("ㅎ");
		mButtonHiEud.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(18);
			}
		});
		mButtonHiEud.setBounds(370, 154, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonHiEud);
		
		mButtonClearLastWord = new JButton("지우기");
		mButtonClearLastWord.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(-3);
			}
		});
		mButtonClearLastWord.setBounds(460, 154, 94, 59);
		mKoreanKeyboardContentPane.add(mButtonClearLastWord);
		
		mButtonAnYe = new JButton("ㅏㅑ");
		mButtonAnYe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(19);
			}
		});
		mButtonAnYe.setBounds(10, 224, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonAnYe);
		
		mButtonAYeo = new JButton(" ㅓㅕ");
		mButtonAYeo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(23);
			}
		});
		mButtonAYeo.setBounds(100, 224, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonAYeo);
		
		mButtonOYo = new JButton("ㅗㅛ");
		mButtonOYo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(27);
			}
		});
		mButtonOYo.setBounds(190, 224, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonOYo);
		
		mButtonUYeu = new JButton("ㅜㅠ");
		mButtonUYeu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(32);
			}
		});
		mButtonUYeu.setBounds(280, 224, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonUYeu);
		
		mButtonEiEu = new JButton("ㅣㅡㅢ");
		mButtonEiEu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    appendText(39);
			}
		});
		mButtonEiEu.setBounds(370, 224, 80, 59);
		mKoreanKeyboardContentPane.add(mButtonEiEu);
		
		mButtonClearAll = new JButton("초기화");
		mButtonClearAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mTextFieldKoreanWord.setText(EMPTY_STRING);
				clear();
			}
		});
		mButtonClearAll.setBounds(460, 224, 94, 59);
		mKoreanKeyboardContentPane.add(mButtonClearAll);
		
		mButtonEnter = new JButton("에이터");
		mButtonEnter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String text = mTextFieldKoreanWord.getText();
				mParentFrame.getTextFieldFindVocab().setText(text);
				mVirtualKeyboardInstance.setVisible(false);
				mVirtualKeyboardInstance.clear();
			}
		});
		mButtonEnter.setBounds(460, 11, 94, 62);
		mKoreanKeyboardContentPane.add(mButtonEnter);
	}

	private void clear() {
		mAutomata.clear();
		mTextFieldKoreanWord.setText(EMPTY_STRING);
	}	
	
	private void appendText(int nCode) {
		mAutomata.setKeyCode(nCode);
		String sText = mAutomata.completeText;
		if(mAutomata.ingWord != '\u0000') {
			sText += mAutomata.ingWord;
		}
		mTextFieldKoreanWord.setText(sText);
		mTextFieldKoreanWord.requestFocus();
	}
	
}
